/*
 *Description:
 *User:laoHu
 *Created with Intellij IDEA 2025
 *Data:2025.05.00
 *Time:00:00
 */
import java.util.Arrays;

public class Main{

    //不规则二维数组
    public static void main(String[] args) {

        int[][] arrays = new int[2][];//行为2，列创建时可以省略
        arrays[0] = new int[1];//第一行有1个元素
        arrays[1] = new int[3];//第二行有3个元素

        //初始化
        arrays[0][0] = 1;
        for (int i = 1; i <= arrays[1].length; i++) {

            arrays[1][i-1] = i ;
        }

        //输出
        for (int[] tem : arrays){
            for (int n : tem){
                System.out.print(n + " ");
            }
            System.out.println();
        }
    }

    //二维数组的使用
    public static void main9(String[] args) {

        //创建数组
        int[][] arrays = new int[2][3];

        //初始化数组
        for (int i = 0;i < arrays.length;i++){

            for (int j = 0; j < arrays[i].length; j++) {

                arrays[i][j] = i + j;
            }
        }

        //遍历数组
        for (int[] tem : arrays){//将arrays引用赋给row

            for (int n : tem) {//将tem指向的成员赋给n

                System.out.print(n + " ");//输出n
            }
            System.out.println();
        }
    }

    public static void main8(String[] args) {
        //arrays是一个引用
        int[] arrays = new int[3];

        //通过下标的方式可以找到引用指向的对象
        arrays[0] = 6;
        arrays[1] = 6;
        arrays[2] = 6;

        //将array这个引用赋值给arrays这个引用
        int[] arrays1 = arrays;

        System.out.println(arrays1[0]);
        System.out.println(arrays1[1]);
        System.out.println(arrays1[2]);

    }

    public static void main7(String[] args) {
        //arrays是一个引用
        int[] arrays = new int[10];

        //通过下标的方式可以找到引用指向的对象
        arrays[0] = 1;
        arrays[1] = 2;
        arrays[2] = 3;

        System.out.println(arrays[0]);
        System.out.println(arrays[1]);
        System.out.println(arrays[2]);

    }

    public static void main6(String[] args) {

        int[] arrays = {10,9,8,7,6,5,4,3,2,1};
        Arrays.sort(arrays);//调用Arrays的sort方法
        //排序后输出
        for (int x : arrays){
            System.out.print(x + " ");
        }
    }

    //冒泡排序
    public static void BubbleSort(int[] arrays){

        for (int i = 0; i < arrays.length - 1; i++) {

            for (int j = 0; j < arrays.length -1 - i; j++) {
                //比较
                if(arrays[j] > arrays[j+1]){
                    int tem = arrays[j];
                    arrays[j] = arrays[j+1];
                    arrays[j+1] = tem;
                }
            }
        }
    }

    public static void main5(String[] args) {

        int[] arrays = {10,9,8,7,6,5,4,3,2,1};
        //使用冒泡排序改为升序
        BubbleSort(arrays);
        //排序后输出
        for (int x : arrays){
            System.out.print(x + " ");
        }
    }

    //二分查找
    public static int BinarySort(int[] arrays,int t){

        int left = 0;//左下标
        int right = arrays.length - 1;//右下标

        while(left < right){
            int mid = (left + right)/2;//中间元素下标

            //中间元素小于目标元素，说明查找元素在右边
            if(arrays[mid] < t){
                left = mid + 1;
                //查找元素在左边
            }else if( arrays[mid] > t){
                right = mid - 1;
            }else{
                return mid;//找到返回下标
            }
        }
        return -1;//找不到返回-1
    }

    public static void main4(String[] args) {

        int[] arrays = {1,2,3,4,5,6,7,8,9,10};
        int target = 5;
        int r =  BinarySort(arrays,target);
        if(r == -1){
            System.out.println("找不到查找的元素....");
        }else{
            System.out.println("找到了，下标为：" + r);
        }
    }

    public static void reverse(char[] chars){

        //通过下标的方式进行访问
        int left = 0;//左下标
        int right = chars.length - 1;//有下标
        while(left < right){

            char tem = chars[left];
            chars[left] = chars[right];
            chars[right] = tem;
            left++;
            right--;
        }

    }
    public static void main3(String[] args) {

        char[] chars = {'a','b','c','d','e','f'};
        reverse(chars);
        System.out.println(Arrays.toString(chars));
    }

    public static void main2(String[] args) {

        int[] arrays = new int[10];
        //使用for循环初始化:1 - 10
        for (int i = 0;i < arrays.length;i++){
            arrays[i] = i + 1;
        }
        //遍历数组：输出1-10
        for (int j = 0;j<arrays.length;j++){

            System.out.print(arrays[j] + " ");
        }
        System.out.println();
        System.out.println("--------------------");
        //使用for-each:将arrays数组的值赋给x后输出
        for (int x: arrays) {

            System.out.print(x + " ");
        }
    }
    public static void main1(String[] args) {

        //1.数组的创建：类型 + 数组名
        // new是一个关键字，表示在堆区开辟一块新的空间
        int[] array1 = new int[2];
        char[] array2 = new char[6];
        float[]  array3 = new float[5];

        //2.创建的同时初始化
        int[] array4 = new int[10];//默认为0
        char[] array5 = {'a','b','c','d','e','f'};
    }

}